package com.BookMate.service.impl;

import com.BookMate.domain.Book;
import com.BookMate.domain.Record;
import com.BookMate.domain.User;
import com.BookMate.mapper.BookMapper;
import com.BookMate.mapper.UserMapper;
import com.BookMate.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户接口实现类
 */
@Service
public class UserServiceImpl implements UserService {
    //注入userMapper
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private BookMapper bookMapper;
    //通过User的用户账号和用户密码查询用户信息

    public User login(User user) {
        return userMapper.login(user);
    }

    public Integer addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public PageResult search(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<User> page = userMapper.searchUsers(user);
        return new PageResult(page.getTotal(), page.getResult());
    }


    @Override
    public Integer banUserid(String id) {
        //根据图书id查询图书的完整信息
        User user = this.findById(id);
        //将图书的借阅状态修改为可借阅
        user.setStatus("1");

        Integer count = userMapper.editUser(user);
        if (count == 1) {
            return 1;
        }
        //如果归还确认成功，则新增借阅记录
        return 0;
    }

    @Override
    public User findById(String id) {
        return userMapper.findById(id);
    }

    @Override
    public Integer disbanUserid(String id) {
        User user = this.findById(id);
        //将图书的借阅状态修改为可借阅
        user.setStatus("0");

        Integer count = userMapper.editUser(user);
        if (count == 1) {
            return 1;
        }
        //如果归还确认成功，则新增借阅记录
        return 0;
    }

    @Override
    public boolean deleteUser(String id) {
        boolean flag = bookMapper.check(id);
        if (flag) return false;
        return userMapper.deleteUser(id);
    }

    @Override
    public void edit(User user) {
        userMapper.editUser(user);
    }
}
